package com.apps.ftapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;

import com.apps.ftapp.R;
import com.apps.ftapp.adapter.ZhenTiNewAdapter;
import com.apps.ftapp.bean.IndexModel;
import com.apps.ftapp.model.ZhenTiModel;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cz.msebera.android.httpclient.Header;

public class ZhenTiNew2Activity extends BaseActivity {


    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private Gson gson = new Gson();

    private List<IndexModel.IndexBean> dataList = new ArrayList();


    private String[] titles = {"模考", "真题"};


    private ZhenTiNewAdapter zhenTiNewAdapter;
    private int flag = 0;//flag=1 模考  flag=2 真题


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhen_ti_new);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        flag = intent.getIntExtra("flag", 0);

        if (flag == 2) {
            setTitle("真题列表");
        } else if (flag == 1) {
            setTitle("模考列表");
        }


        initView();
        loadData();


    }

    private void initView() {

        zhenTiNewAdapter = new ZhenTiNewAdapter(getApplicationContext(), dataList);
        listView.setAdapter(zhenTiNewAdapter);


    }


    private void loadData() {
        RequestParams params = new RequestParams();
        params.put("token", Constant.token);
        params.put("userId", Constant.uid);
        Async.post("testMode/list", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", ">>>>>" + new String(responseBody));
                    ZhenTiModel result = new ZhenTiModel();
                    try {
                        result = gson.fromJson(new String(responseBody), ZhenTiModel.class);
                    } catch (Exception e) {

                    }

                    List<IndexModel.IndexBean> dList = new ArrayList<IndexModel.IndexBean>();
                    if (1 == Tools.isIntNull(result.getSuccess())) {
//                        dataList.addAll(result.getTestpapers());
//                        zhenTiAdapter.addRes(result.getTestpapers());


                        for (int i = 0; i < titles.length; i++) {

                            if ((flag == 1 && i == 0) || (flag == 2 && i == 1)) {
                                IndexModel.IndexBean indexBean1 = new IndexModel.IndexBean();
                                indexBean1.setTitle(titles[i]);
                                indexBean1.setSubTitle(titles[i]);
                                indexBean1.setImage("");
                                indexBean1.setTimes("");
                                indexBean1.setBiaoqian("");
                                indexBean1.setId(i + "");
                                indexBean1.setIstop("");
                                indexBean1.setType(0);
                                dList.add(indexBean1);
                            }

                            switch (i) {

                                case 0:
                                    //模考
                                    if (flag == 1) {
                                        List<ZhenTiModel.MoniBean> moList = result.getMoni();
                                        if (null != moList & moList.size() > 0) {
                                            for (ZhenTiModel.MoniBean m : moList) {
                                                IndexModel.IndexBean indexBean15 = new IndexModel.IndexBean();

                                                indexBean15.setTitle(m.getYear() + "");

                                                indexBean15.setType(1);

                                                List<Map> mapList = new ArrayList<Map>();
                                                if (null != m.getTestpapers() && m.getTestpapers().size() > 0) {
                                                    for (ZhenTiModel.MoniBean.TestpapersBean testpapersBean : m.getTestpapers()) {

                                                        Map mm = new HashMap();
                                                        mm.put("id", testpapersBean.getId());
                                                        mm.put("title", testpapersBean.getName());
                                                        mapList.add(mm);
                                                    }
                                                    indexBean15.setDataList(mapList);

                                                    dList.add(indexBean15);
                                                }
                                            }
                                        }
                                    }
                                    break;


                                case 1:
                                    //真题
                                    if (flag == 2) {
                                        List<ZhenTiModel.ZhentiBean> zhentiBeanList = result.getZhenti();
                                        if (null != zhentiBeanList & zhentiBeanList.size() > 0) {
                                            for (ZhenTiModel.ZhentiBean m : zhentiBeanList) {
                                                IndexModel.IndexBean indexBean15 = new IndexModel.IndexBean();

                                                indexBean15.setTitle(m.getYear() + "");

                                                indexBean15.setType(1);

                                                List<Map> mapList = new ArrayList<Map>();
                                                if (null != m.getTestpapers() && m.getTestpapers().size() > 0) {
                                                    for (ZhenTiModel.ZhentiBean.TestpapersBeanX testpapersBean : m.getTestpapers()) {

                                                        Map mm = new HashMap();
                                                        mm.put("id", testpapersBean.getId());
                                                        mm.put("title", testpapersBean.getName());
                                                        mapList.add(mm);
                                                    }
                                                    indexBean15.setDataList(mapList);

                                                    dList.add(indexBean15);
                                                }

                                            }

                                        }
                                    }
                                    break;
                            }


                        }


                        dataList.addAll(dList);
                        zhenTiNewAdapter.addRes(dList);
                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                L.i("main", ">>>>>" + new String(responseBody));
            }

            @Override
            public void onFinish() {
                // listView.getChildAt(0).setVisibility(View.GONE);
                super.onFinish();
            }
        });
    }


}

